<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>基本资料</span>
    </div>
    <!-- 表单 -->
    <el-form
      :model="userForm"
      status-icon
      :rules="userRules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="登录名称">
        <el-input
          v-model="userForm.username"
          disabled
          type="text"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="用户昵称" prop="nickname">
        <el-input
          v-model="userForm.nickname"
          type="text"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="用户邮箱" prop="email">
        <el-input
          autocomplete="off"
          type="text"
          v-model="userForm.email"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="submitHeadler" type="primary">提交</el-button>
        <el-button @click="resetHeadler">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
export default {
  name: 'UserInfo',
  data () {
    return {
      //   userForm: Object.assign({}, this.$store.state.userInfo),
      userForm: { ...this.$store.state.userInfo },
      userRules: {
        nickname: [
          { required: true, message: '请输入用户昵称', trigger: 'blur' },
          {
            pattern: /^\S{1,10}$/,
            message: '用户昵称必须是1-10的非空字符',
            trigger: 'blur'
          }
        ],
        email: [
          {
            type: 'email',
            message: '邮箱格式不正确',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    resetHeadler () {
      this.userForm = { ...this.$store.state.userInfo }
      this.$refs.ruleForm.clearValidate()
    },
    submitHeadler () {
      this.$refs.ruleForm.validate(async (result) => {
        if (!result) return
        await this.$http.put('/my/userinfo', this.userForm)

        this.$store.dispatch('getUserInfo')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.el-form {
  width: 500px;
}
</style>
